package com.yohann.eduorder.controller;


import com.yohann.commonutils.result.R;
import com.yohann.eduorder.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author Yohann
 * @since 2020-08-28
 */
@RestController
@RequestMapping("/order-service/paylog")
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    /**
     * 生成支付二维码
     * @param orderNo
     * @return
     */
    @GetMapping("createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo){
        Map map = payLogService.createNative(orderNo);

        return R.ok().data(map);
    }

    /**
     * 查询支付状态
     * @param orderNo
     * @return
     */
    @GetMapping("getPayStatue/{orderNo}")
    public R getPayStatus(@PathVariable String orderNo){
        Map<String,String> map = payLogService.getPayStatus(orderNo);

        if (map.isEmpty()){
            return R.error().message("支付失败");
        }

        //如果返回不为空，查询支付状态
        if (map.get("trade_state").equals("SUCCESS")){
            //添加支付记录 修改订单状态
            payLogService.updateOrderStatue(map);
            return R.ok().message("支付成功");
        }

        return R.ok().code(25000).message("支付中");
    }
}

